<?php
class Dieta extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('dieta_model');
		$this->load->library('form_validation');
	}

	public function view($id)
	{		
		$this->form_validation->set_rules('ingrediencie', 'Ingrediencie', 'required');
		
		if ($this->form_validation->run())
		{
			$this->dieta_model->vymazanie_diety($id);
			$this->dieta_model->pridanie_ingrediencii($_POST['ingrediencie']);
			$this->dieta_model->ingrediencie_do_diety($_POST['ingrediencie'], $id);
			if ($this->session->userdata('pacient') != 'empty')
				redirect('jedalnicek/vypis/'.$this->session->userdata('pacient'));
			else
				redirect('pacienti');
		}		
		
		$data['title'] = 'NIS - Diéta';
		$data['ingrediencie'] = $this->dieta_model->ingrediencie($id);
		$name = $this->dieta_model->ziskaj_nazov($id);
		$data['dieta'] = str_replace('-', ' ', $name);
		$data['id'] = $id;
		
		$this->load->view('templates/header', $data);
		$this->load->view('project/dieta_view', $data);	
		$this->load->view('templates/footer');
		
	}
	
	public function pridaj()
	{
		$this->form_validation->set_rules('name', 'Názov', 'required');
		$this->form_validation->set_rules('items', 'Ingrediencie', 'required');
		
		if ($this->form_validation->run())
		{
			//ked prebehne formular akcia a potom redirect
			$name = str_replace(' ', '-', $_POST['name']);
			$this->dieta_model->nova_dieta($name);
			$this->dieta_model->pridanie_ingrediencii($_POST['items']);
			$id = $this->dieta_model->ziskaj_id($name);
			$this->dieta_model->ingrediencie_do_diety($_POST['items'], $id);
			redirect('pacienti');		
		}		
		$data['title'] = 'NIS - Pridanie diéty';
		$this->load->view('templates/header', $data);
		$this->load->view('project/pridanie_diety_view');
		$this->load->view('templates/footer');
	}
}